--[[
    Author          ZhengFeng
    Time            2020.12.25
    Description     玩家宠物信息界面
]]--
require "Common/define"
require "Logic/PetManager"

local Page_PetInfoDetail = UIBase.New(PanelResNames.Page_PetInfoDetail)
local this = Page_PetInfoDetail
local partListIndex = 0

function this:Init()
    UIBase.Init(this)
end

function this:OnCreate()
    self.Btn_BGClose = self:GetChild("Btn_BGClose")
    self.Txt_PetName = self:GetChild("Txt_PetName")
    self.List_Part = self:GetChild("List_Part")
    self.List_Part.itemRenderer = this.renderPartListItem
    self.Model_Pet = self:GetChild("Model_Pet")
    
    self:RegisterOnClick(self.Btn_BGClose,self.onClickClose)
end

function this:OnEnable()
    this:showNewBornPet()
end

function this:OnDisable()
    this:removePetModel()
end
function this:OnDestroy()
    this:removePetModel()
end

function this:onClickClose()
    this:removePetModel()
    closeUI(PanelResNames.Page_PetInfoDetail)
end

function this:showNewBornPet()
    local _petInfo = PetManager.OtherPetInfo
    if _petInfo  then
        self.Txt_PetName.text = _petInfo.petName

        partListIndex = 0
        self.List_Part.numItems = #_petInfo.genes

        this:showPetModel()
    end
end
function this:renderPartListItem(obj)
    partListIndex = partListIndex + 1
    local _petInfo = PetManager.OtherPetInfo
    local _geneId = _petInfo.genes[partListIndex]
    local ctrlItem = CtrlManager.GetCtrl(PanelNames.PetPartItem);
    ctrlItem.initData(obj, _geneId);

    --local _geneConfig = G.dataTable["t_elvesgene"]
    --local _geneInfo = _geneConfig[tostring(_geneId)]
    --local _partName = getLanguage(_geneInfo.f_GeneName)
    --
    --local Txt_title = obj:GetChild("title")
    --Txt_title.text = _partName
    --
    --local _petMutationLevel = _geneInfo.f_MutationLevel
    --obj:GetController("level"):SetSelectedIndex(_petMutationLevel - 1)
end

--显示宠物模型
function this:removePetModel()
    if this.PetModelGO then
        destroy(this.PetModelGO)
    end
    this.PetModelGO = nil
end
function this:showPetModel()
    local _petInfo = PetManager.OtherPetInfo
    if _petInfo and #_petInfo.genes == PetManager.PetPart.Ornament then
        local _petModelInfo = {}
        _petModelInfo.parentGO = this.Model_Pet
        _petModelInfo.petId = _petInfo.petId
        if this.PetModelGO and this.goWrapper then
            _petModelInfo.modelGO = this.PetModelGO
            _petModelInfo.modelWrapper = this.goWrapper
            PetManager.PetModelRefresh(_petModelInfo,_petInfo.genes,true)
        else
            this:removePetModel()
            resMgr:LoadModelPrefab(PetManager.PetModelPrefab,function(go)
                this.PetModelGO = newObject(go)

                local com = this.PetModelGO:GetComponent("SkeletonGraphic")

                if this.PetModelGO and com then
                    com.material = resMgr.Pet_Copy_Mat
                    com.material:SetFloat("_StencilComp",8)
                    com.material:SetFloat("_Stencil",1);
                end
                
                _petModelInfo.modelGO = this.PetModelGO
                local _modelWrapper = this.goWrapper
                this.goWrapper = GoWrapper.New()
                _petModelInfo.modelWrapper = this.goWrapper
                PetManager.PetModelFirstSet(_petModelInfo,_petInfo.genes,true,Vector3.New(0,0,0),Vector3.New(3,3,1))
                if _modelWrapper then
                    _modelWrapper:Dispose()
                end
            end)
        end
    end
end
return this